﻿

namespace Mulala.Cavan.Presentation.Mvc.Controller
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using Mulala.Cavan.Application.MainBoundedContext.CorporationModule;
    using Mulala.Cavan.Application.MainBoundedContext.MulalaModule;
    using Mulala.Cavan.Application.MainBoundedContext.SecurityModule;
    using Mulala.Cavan.Application.Seedwork;
    using Mulala.Cavan.Domain.MainBoundedContext.CorporationModule;
    using Mulala.Cavan.Domain.MainBoundedContext.DictionaryModule;
    using Mulala.Cavan.Domain.MainBoundedContext.SearchModule;
    using Mulala.Cavan.Domain.MainBoundedContext.SecurityModule;
    using Mulala.Cavan.Domain.MainBoundedContext.Setting;
    using Mulala.Cavan.Presentation.Mvc.ViewModel;
    using Mulala.Cavan.Presentation.Seedwork;
    using Mulala.Cavan.Domain.MainBoundedContext.NannyModule;
    using Mulala.Cavan.Application.MainBoundedContext.NannyModule;

    public class HomeController : EntityController<UserAccount, UserAccountSearch, IUserAccountViewModel, IUserAccountAppService>, IHomeController
    {
        private ICorporationAppService CorporationAppService { get; set; }
        private INannyAppService NannyAppService { get; set; }
        private IAreaAppService AreaAppService { get; set; }
        private ITagAppService TagAppService { get; set; }

        public HomeController()
        {
            this.Model = ModelFactory.CreateModel<IUserAccountViewModel>();
        }

        public ActionResult Index()
        {
            IHomeViewModel homeViewModel = ModelFactory.CreateModel<IHomeViewModel>();
            CorporationSearch corporationSearch = CorporationSearchFactory.CreateCorporationSearch(CorporationIdentifier.OnLineStatus.OnLine);
            homeViewModel.CorporationTotal = this.CorporationAppService.GetCount(corporationSearch);
            NannySearch nannySearch = NannySearchFactory.CreateNannySearch(NannyIdentifier.NannyType.Nanny, NannyIdentifier.OnLineStatus.OnLine);
            homeViewModel.NannyTotal = this.NannyAppService.GetCount(nannySearch);
            nannySearch = NannySearchFactory.CreateNannySearch(NannyIdentifier.NannyType.Nurse, NannyIdentifier.OnLineStatus.OnLine);
            homeViewModel.NurseTotal = this.NannyAppService.GetCount(nannySearch);

            return View(homeViewModel);
        }

        public ActionResult About()
        {
            ViewBag.Message = "Your app description page.";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }

        public override string ControllerName
        {
            get { return "Home"; }
        }

        public override bool UseDenied
        {
            get { return false; }
        }

        [ActionName("topsection")]
        [ChildActionOnly]
        public ActionResult GetTopSection()
        {
            ITopSectionViewModel topSectionViewModel = ModelFactory.CreateModel<ITopSectionViewModel>();
            if (this.Model.MasterModel.CurrentUserModel.UserAccountID > 0)
            {
                if (this.Model.MasterModel.CurrentUserModel.UserRole == SecurityIdentifier.Role.Corporation)
                {
                    topSectionViewModel.UserCenterUrl = Project.Settings.HostServer + "/corporationcenter/index";
                }
                else if (this.Model.MasterModel.CurrentUserModel.UserRole == SecurityIdentifier.Role.Nanny)
                {
                    topSectionViewModel.UserCenterUrl = Project.Settings.HostServer + "/nannycenter/index";
                }
                else if (this.Model.MasterModel.CurrentUserModel.UserRole == SecurityIdentifier.Role.Proprietor)
                {
                    topSectionViewModel.UserCenterUrl = Project.Settings.HostServer + "/proprietorcenter/index";
                }
            }

            return PartialView("~/Views/Shared/_topSectionPartial.cshtml", topSectionViewModel);
        }

        [ActionName("searchbox")]
        [ChildActionOnly]
        public ActionResult GetSearchBox()
        {
            ISearchBoxViewModel searchBoxViewModel = ModelFactory.CreateModel<ISearchBoxViewModel>();

            IDictionary<string, bool> orderBy = new Dictionary<string, bool>();
            orderBy.Add("ViewTotal", true);
            CorporationSearch corporationSearch = CorporationSearchFactory.CreateCorporationSearch(orderBy, 1, Project.Settings.HotCorporationCount);
            IList<Corporation> corporations = this.CorporationAppService.GetBySearch(corporationSearch).ToList();
            searchBoxViewModel.HotCorporations = corporations;

            AreaSearch areaSearch = AreaSearchFactory.CreateAreaSearch(CavanWebUtility.GetUserSelectedCity(), true);
            IList<Area> towns = this.AreaAppService.GetBySearch(areaSearch).Take(Project.Settings.StartAreaCount).ToList();
            searchBoxViewModel.StartAreas = towns;

            TagSearch tagSearch = TagSearchFactory.CreateTagSearch(DictionaryIdentifier.GroupType.NannyExperience);
            IList<Tag> tags = this.TagAppService.GetBySearch(tagSearch).ToList();
            searchBoxViewModel.StartExperiences = tags;

            return PartialView("~/Views/Home/_searchBoxPartial.cshtml", searchBoxViewModel);
        }
        
    }
}
